//
//  bargainInterfaceViewController.m
//  INAlienLand
//
//  Created by dllo on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "bargainInterfaceViewController.h"
#import "bargainModel.h"
#import "HTTPTool.h"
#import "downTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "secondViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface bargainInterfaceViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *bigTableView;

@property(nonatomic,retain)NSMutableArray *arr;

@property(nonatomic,retain)UIButton *button1;

@property(nonatomic,retain)UIButton *button2;

@property(nonatomic,retain)UIButton *button3;

@property(nonatomic,retain)UIButton *button4;

@property(nonatomic,retain)UIButton *mybutton;

@end

@implementation bargainInterfaceViewController

- (void)dealloc
{
    [_button1 release];
    [_button2 release];
    [_button3 release];
    [_button4 release];
    [_mybutton release];
    [_arr release];
    [_bigTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"特价";
//    self.titleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) style:UITableViewStylePlain];
//    self.titleTableView.tag = 1000;
//    [self.view addSubview:self.titleTableView];
    
    //4个Button
//    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self.button1 setTitle:@"产品类型" forState:UIControlStateNormal];
//    [self.button1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:self.button1];
//    
//    self.button2 = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self.button2 setTitle:@"目的地" forState:UIControlStateNormal];
//    [self.view addSubview:self.button2];
//    
//    self.button3 = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self.button3 setTitle:@"出发城市" forState:UIControlStateNormal];
//    [self.view addSubview:self.button3];
//    
//    self.button4 = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self.button4 setTitle:@"出发时间" forState:UIControlStateNormal];
//    [self.view addSubview:self.button4];
    
//    self.button1.frame = CGRectMake(0, 0, self.view.frame.size.width/4, 50);
//    self.button2.frame = CGRectMake(WIDTH/4, 0, WIDTH/4, 50);
//    self.button3.frame = CGRectMake(WIDTH/2, 0, WIDTH/4, 50);
//    self.button4.frame = CGRectMake(WIDTH * 3 / 4, 0, WIDTH/4, 50);
    self.mybutton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.mybutton.frame = CGRectMake(0, 0, WIDTH, 50);
    self.mybutton.backgroundColor = [UIColor whiteColor];
    [self.mybutton setTitle:@"在      他      乡" forState:UIControlStateNormal];
    self.mybutton.titleLabel.font = [UIFont systemFontOfSize:22];
    [self.view addSubview:self.mybutton];
    [self.mybutton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    self.arr = [NSMutableArray array];
    [self createData];
    
    self.bigTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 64 - 49 - 50) style:UITableViewStylePlain];
    [self.view addSubview:self.bigTableView];
    [_bigTableView release];
    self.bigTableView.delegate = self;
    self.bigTableView.dataSource = self;
    self.bigTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [self.bigTableView addFooterWithCallback:^{
        [self createData];
        [self.bigTableView footerEndRefreshing];
    }];
    
    
    
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    secondViewController *sec = [[secondViewController alloc] init];
    bargainModel *model = self.arr[indexPath.row];
    sec.url = model.url;
    [self.navigationController pushViewController:sec animated:YES];
}

- (void)click:(UIButton *)button{
    self.mybutton.backgroundColor = [UIColor colorWithRed:arc4random()%256 / 255.00 green:arc4random()%256 / 255.00 blue:arc4random()%256 / 255.00 alpha:1.00];
}

- (void)createData{
    static NSInteger i = 1 ;
    i++;
    NSString *count =[NSString stringWithFormat:@"http://breadtrip.com/tp/products/?category=&dest=&start=&depart=&page=%ld",i];
    [HTTPTool get:count body:nil HTTPResult:JSON success:^(id result) {
                NSDictionary *dic = result;
        NSArray *arr = dic[@"products"];
        for (NSDictionary *tempDic in arr) {
            bargainModel *Model = [[bargainModel alloc] init];
            [Model setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject:Model];
//            NSLog(@"%ld",Model.min_price);
        }
        [self.bigTableView reloadData];
    } failure:^(NSError *error) {
        
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return self.arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 150;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    downTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[downTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    bargainModel *model = self.arr[indexPath.row];
    
    [cell.leftimageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
    cell.titleLabel.text = model.title;
    cell.depart_place.text = model.depart_place;
    cell.min_price.text = [NSString stringWithFormat:@"%ld元起",model.min_price];
    cell.market_price.text = [NSString stringWithFormat:@"市场价:  %ld",model.market_price];
    NSString *str = @"";
    NSString *str1 = @" ";
    for (NSString *temp in model.depart_dates) {
        str =[str stringByAppendingString:temp];
        str =[str stringByAppendingString:str1];
    }
    str = [NSString stringWithFormat:@"出发日期: %@",str];
    cell.depart_dates.text = str;
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
